package cn.mcch.investigator.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ExpandableListView;

import cn.mcch.investigator.customview.pulltorefresh.PullToRefreshLayout;
import cn.mcch.investigator.listener.OnCommonClickListener;
import cn.mcch.investigator.model.BaseModel;
import cn.mcch.investigator.presenter.BasePresenter;
import cn.mcch.investigator.viewdelegate.BaseViewDelegate;

public abstract class BaseFragment<T extends BaseViewDelegate, M extends BaseModel, P extends BasePresenter<T, M>>
		extends Fragment implements OnCommonClickListener {

	public P mPresenter;
	protected boolean isLoadData;

	public boolean isLoadData() {
		return isLoadData;
	}

	/**
	 * 构造方法
	 */
	public BaseFragment() {
		try {
			// 初始化Presenter

			mPresenter = getPresenterClass().newInstance();
		} catch (Exception e) {
			throw new RuntimeException("create BaseViewDelegate error!");
		}
	}

	public abstract Class<P> getPresenterClass();

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
			@Nullable Bundle savedInstanceState) {
		// 创建根布局
		mPresenter.createRootView(getContext());
		// 初始化actionbar
		initTitleBar();
		// 初始化content
		mPresenter.initConetentView(getContext());
		// 创建界面要展示的第一个布局
		createFirstView();
		return mPresenter.getRootView();
	}

	/**
	 * 重写该方法 调用presenter.initTitleBar
	 */
	public void initTitleBar() {

	}

	/**
	 * 重写该方法 调用presenter.createFirstView
	 */
	public abstract void createFirstView();

	// 请求数据，需要时重写，调用presenter的initData
	public void initData() {
		isLoadData = true;

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == getResources().getIdentifier("bt_load_again", "id", getActivity().getPackageName())) {
			// 重新加载数据
			isLoadData = false;
			createFirstView();
			initData();
		}
	}

	@Override
	public boolean onLongClick(View v) {
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		return false;
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
		return false;
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

	}

	@Override
	public void onPageSelected(int position) {

	}

	@Override
	public void onPageScrollStateChanged(int state) {

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mPresenter != null) {
			mPresenter.clear();
		}
	}

	@Override
	public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {

	}

	@Override
	public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

	}
}
